package org.example.mybatis.plus.boot3.controller;

import org.example.mybatis.plus.boot3.entity.User;
import org.example.mybatis.plus.boot3.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UseController {
    @Autowired
    private UserService userService;

    @GetMapping
    public List list(){
        return userService.list();
    }

    @GetMapping("/{id}")
    public User get(@PathVariable Long id){
        return userService.getById(id);
    }

    @PostMapping
    public String save(@RequestBody User user){
        boolean result = userService.save(user);
        return result ? "保存成功" : "保存失败";
    }

    @PostMapping("/batch")
    public String saveBatch(@RequestBody List<User> users){
        boolean result = userService.saveBatch(users,2);
        return result ? "保存成功" : "保存失败";
    }

    @PostMapping("/saveOrUpdate")
    public String saveOrUpdate(@RequestBody User user){
        boolean result = userService.saveOrUpdate(user);
        return result ? "更新成功" : "更新失败";
    }

    @PostMapping("/saveOrUpdate/batch")
    public String saveOrUpdateBatch(@RequestBody List<User> users){
        boolean result = userService.saveOrUpdateBatch(users,2);
        return result ? "更新成功" : "更新失败";
    }

    @DeleteMapping("/{id}")
    public String delete(@PathVariable Long id){
        boolean result = userService.removeById(id);
        return result ? "删除成功" : "删除失败";
    }

    @GetMapping("/name/{name}")
    public List<User> getUsersByName(@PathVariable String name) {
        return userService.getByNameWithLambda(name);
    }

    @PutMapping("/update")
    public String updateByName(@RequestParam String name,@RequestParam String newName){
        boolean result = userService.updateByNameWithLambda(name, newName);
        return result ? "更新成功" : "更新失败";
    }

    @GetMapping("/wrapper")
    public String wrapper(){
        userService.wrapper();
        return "成功";
    }



}
